<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 16:53
 */

namespace app\api\controller;

use app\common\controller\Api;
class Outapi extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function bookInfo(){
        $apikey = input("apikey");
        $outapi = db("outapi")->where("apikey",$apikey)->find();
//        dump($outapi['book_ids']);
        $arr_data = explode("/",$outapi['book_ids']);
//        dump($arr_data);
        $arr_book = [];
        foreach ($arr_data as $k=>$v){
            $find_book_data = db("book")->where("id",$v)->find();
            if(empty($find_book_data)) continue;
            $arr['book_id'] = $find_book_data['id'];
            $arr['book_name'] = $find_book_data['title'];
            $arr['cate'] = $find_book_data['name'];
            $arr_book[] = $arr;
        }
        $this->success('success',$arr_book);
    }
    public function chapter(){
        //检查api是存在
        $apikey = input("apikey");
        $book_id = input("book_id");
        if(empty($apikey)) $this->error("apikey不能为空",null,-204);

        $outapi = db("outapi")->where("apikey",$apikey)->find();
        if(empty($outapi)) $this->error("apikey error",null,-205);

        $arr_data = explode("/",$outapi['book_ids']);
        $is_set = in_array($book_id,$arr_data);
        if(!$is_set) $this->error("书籍ID不正确",null,-206);
        $chapter_content_data = db("bookinfo")->where("bid",$book_id)->paginate(10)->toArray();
//        dump($chapter_content_data['data']);die;
        if(empty($chapter_content_data['data'])) $this->error("章节内容为空",null,-208);
        $arr_data = [];
        foreach ($chapter_content_data['data'] as $k=>$v){
            $arr['chapter_name'] = $v['title2'];
            $arr['chapter_content'] = $v['content'];
            $arr['update_time'] = $v['time'];
            $arr['is_vip'] = $v['pay'];
            $arr['index'] = $v['number'];
            $arr_data[] = $arr;
        }
        if(!empty($arr_data)) $this->success($arr_data);
    }
    public function index(){
        /*$id = input("id");
//        $data = $this->getCateByPid($id);
//        dump($data);die;
        $find_data = db("auth_rule")->where("id",$id)->find();
        dump($find_data);
        $menu_data = $this->dataArr($find_data);
        db('auth_rule')->insertAll($menu_data);*/
//        dump($data);
    }
    public function dataArr($find_data){
        $arr = [
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/add','title'=>'添加',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ],//添加
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/index','title'=>'列表',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ],//列表
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/edit','title'=>'修改',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ],//修改
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/delete','title'=>'删除',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ],//删除
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/detail','title'=>'详情',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ],//详情
            [
                'type'=>'file',
                'pid' => $find_data['id'],
                'name'=>$find_data['name'].'/multi','title'=>'批量',
                'icon' => 'fa fa-circle-o',
                'ismenu' =>0,
                'createtime' => time(),
                'updatetime' => time(),
                'weigh' =>0,
                'status' => 'normal'
            ]//批量*/
        ];
        return $arr;
    }

}